package com.megvii.corelib.datastructure;

import android.support.annotation.NonNull;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by linyue on 16/2/2.
 * 线程不安全的
 */
public class LimitedQueue<E> implements Queue<E>
{
    private LinkedList<E> list;

    private int limitation;

    public LimitedQueue(int limitation)
    {
        list = new LinkedList<>();
        this.limitation = limitation;
    }

    @Override
    public boolean add(E e)
    {
        if (limitation > 0)
        {
            if (list.size() == limitation)
            {
                list.removeFirst();
            }
            list.add(e);
        }
        return true;
    }

    @Override
    public boolean addAll(Collection<? extends E> collection)
    {
        int oldElementsAmount = list.size();
        int newElementsAmount = collection.size();
        int removeAmount = newElementsAmount + oldElementsAmount - limitation;
        if (removeAmount >= limitation)
        {
            list.clear();
            int remainAmount = collection.size();
            Iterator<? extends E> iterator = collection.iterator();
            while (iterator.hasNext())
            {
                E element = iterator.next();
                if (remainAmount <= limitation)
                {
                    list.add(element);
                }
                remainAmount--;
            }
        }
        else
        {
            for (int i = 0; i < removeAmount; i++)
            {
                list.removeFirst();
            }
            list.addAll(collection);
        }
        return false;
    }

    @Override
    public void clear()
    {
        list.clear();
    }

    @Override
    public boolean contains(Object object)
    {
        return list.contains(object);
    }

    @Override
    public boolean containsAll(Collection<?> collection)
    {
        return list.containsAll(collection);
    }

    @Override
    public boolean isEmpty()
    {
        return list.isEmpty();
    }

    @NonNull
    @Override
    public Iterator<E> iterator()
    {
        return list.iterator();
    }

    @Override
    public boolean remove(Object object)
    {
        return list.remove(object);
    }

    @Override
    public boolean removeAll(Collection<?> collection)
    {
        return list.removeAll(collection);
    }

    @Override
    public boolean retainAll(Collection<?> collection)
    {
        return list.retainAll(collection);
    }

    @Override
    public int size()
    {
        return list.size();
    }

    @NonNull
    @Override
    public Object[] toArray()
    {
        return list.toArray();
    }

    @NonNull
    @Override
    public <T> T[] toArray(T[] array)
    {
        return list.toArray(array);
    }

    @Override
    public boolean offer(E e)
    {
        return add(e);
    }

    @Override
    public E remove()
    {
        return list.remove();
    }

    @Override
    public E poll()
    {
        return list.poll();
    }

    @Override
    public E element()
    {
        return list.element();
    }

    @Override
    public E peek()
    {
        return list.peek();
    }

    public E peekTail()
    {
        return list.getFirst();
    }
}
